package com.koron.dynamic.dataSource;

import com.koron.dynamic.service.AppDataSourceService;
import com.zaxxer.hikari.HikariDataSource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

import javax.sql.DataSource;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @ClassName DynamicDataSourceUtil
 * @description
 * @Author zhouj
 * @Date 2021/7/12 18:25
 */
@Slf4j
//@Component
public class DynamicDataSource extends AbstractRoutingDataSource {

    private Object defaultTargetDataSource;

    @Autowired
    AppDataSourceService appDataSourceService;

    private ConcurrentHashMap<String, DataSource> concurrentHashMap = new ConcurrentHashMap<String, DataSource>();

    /**
     * 如果不希望数据源在启动配置时就加载好，可以定制这个方法，从任何你希望的地方读取并返回数据源
     * 比如从数据库、文件、外部接口等读取数据源信息，并最终返回一个DataSource实现类对象即可
     */
    @Override
    protected DataSource determineTargetDataSource() {
        HikariDataSource dataSource = new HikariDataSource();
        //考虑在redis中存，取
        //ThreadLocal 拿存好的数据库标识

        String dataSourceKey = DynamicDataSourceContextHolder.getDataSourceKey();

        if (dataSourceKey.equals("master")) {
            dataSource = (HikariDataSource) super.resolveSpecifiedDataSource(this.defaultTargetDataSource);
            return dataSource;
        }

        DataSource cacheDataSource = concurrentHashMap.get(dataSourceKey);

        if (cacheDataSource != null) {
            return cacheDataSource;
        }

        return cacheDataSource;
        //return super.determineTargetDataSource();
    }

    /**
     * 如果希望所有数据源在启动配置时就加载好，这里通过设置数据源Key值来切换数据，定制这个方法
     */
    @Override
    protected Object determineCurrentLookupKey() {
        return DynamicDataSourceContextHolder.getDataSourceKey();
    }

    /**
     * 设置默认数据源
     *
     * @param defaultDataSource
     */
    public void setDefaultDataSource(Object defaultDataSource) {
        this.defaultTargetDataSource = defaultDataSource;
        super.setDefaultTargetDataSource(defaultDataSource);
    }

    /**
     * 设置数据源
     *
     * @param dataSources
     */
    public void setDataSources(Map<Object, Object> dataSources) {
        super.setTargetDataSources(dataSources);
        // 将数据源的 key 放到数据源上下文的 key 集合中，用于切换时判断数据源是否有效
        DynamicDataSourceContextHolder.addDataSourceKeys(dataSources.keySet());
    }

    public void addDataSources(String key,DataSource dataSource){
        concurrentHashMap.put(key,dataSource);
    }

}
